package com.thh.datamonitor.controller;

import com.thh.common.annotation.Log;
import com.thh.common.utils.StringUtils;
import com.thh.common.utils.bean.BeanUtils;
import com.thh.common.core.controller.BaseController;
import com.thh.common.core.model.AjaxResult;
import com.thh.common.core.model.entity.SysUser;
import com.thh.common.core.page.TableDataInfo;
import com.thh.common.enums.BusinessType;
import com.thh.common.enums.DatabaseUserStatusEnums;
import com.thh.common.exception.ServiceException;
import com.thh.common.utils.ShiroUtils;
import com.thh.common.utils.poi.ExcelUtil;
import com.thh.datamonitor.dto.TmdUserDatabaseDto;
import com.thh.datamonitor.dto.TmdUserDatabaseEditDto;
import com.thh.datamonitor.dto.TmdUserDatabaseQueryDto;
import com.thh.datamonitor.entity.TmdDatabase;
import com.thh.datamonitor.entity.TmdUserDatabase;
import com.thh.datamonitor.service.ITmdDatabaseService;
import com.thh.datamonitor.service.ITmdUserDatabaseService;
import com.thh.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户数据库Controller
 * 
 * @author thh
 * @date 2022-06-23
 */
@Controller
@RequestMapping("/datamonitor/myUserDatabase")
public class MyUserDatabaseController extends BaseController
{
    private String prefix = "datamonitor/myUserDatabase";

    @Autowired
    private ITmdUserDatabaseService tmdUserDatabaseService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ITmdDatabaseService tmdDatabaseService;

    @RequiresPermissions("datamonitor:myUserDatabase:view")
    @GetMapping()
    public String myUserDatabase()
    {
        return prefix + "/myUserDatabase";
    }

    /**
     * 查询用户数据库列表
     */
    @RequiresPermissions("datamonitor:myUserDatabase:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TmdUserDatabaseQueryDto tmdUserDatabaseQueryDto)
    {
        startPage();
        tmdUserDatabaseQueryDto.setUserId(ShiroUtils.getLoginName());
        List<TmdUserDatabaseDto> list = tmdUserDatabaseService.selectMyUserDatabaseList(tmdUserDatabaseQueryDto);
        return getDataTable(list);
    }

    /**
     * 导出用户数据库绑定信息列表
     */
    @RequiresPermissions("datamonitor:myUserDatabase:export")
    @Log(title = "用户数据库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TmdUserDatabaseQueryDto tmdUserDatabaseQueryDto)
    {
        tmdUserDatabaseQueryDto.setUserId(ShiroUtils.getLoginName());
        List<TmdUserDatabaseDto> list = tmdUserDatabaseService.selectMyUserDatabaseList(tmdUserDatabaseQueryDto);
        ExcelUtil<TmdUserDatabaseDto> util = new ExcelUtil<TmdUserDatabaseDto>(TmdUserDatabaseDto.class);
        return util.exportExcel(list, "用户数据库");
    }
    

    /**
     * 新增用户数据库
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户数据库
     */
    @RequiresPermissions("datamonitor:myUserDatabase:add")
    @Log(title = "用户数据库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TmdUserDatabase tmdUserDatabase)
    {
        tmdUserDatabase.setUserId(ShiroUtils.getLoginName());
        String status = tmdUserDatabase.getStatus();
        if (!DatabaseUserStatusEnums.APPROVE.getCode().equals(status) && !DatabaseUserStatusEnums.DRAFT.getCode().equals(status)) {
            throw new ServiceException("状态只能为草稿和审批");
        }
        return toAjax(tmdUserDatabaseService.insertTmdUserDatabase(tmdUserDatabase));
    }

    /**
     * 修改用户数据库
     */
    @RequiresPermissions("datamonitor:myUserDatabase:edit")
    @GetMapping("/edit/{myUserDatabaseId}")
    public String edit(@PathVariable("myUserDatabaseId") Long myUserDatabaseId, ModelMap mmap)
    {
        TmdUserDatabase tmdUserDatabase = tmdUserDatabaseService.selectTmdUserDatabaseByUserDatabaseId(myUserDatabaseId);
        TmdUserDatabaseEditDto tmdUserDatabaseEditDto = null;
        if (StringUtils.isNotNull(tmdUserDatabase)) {
            tmdUserDatabaseEditDto = new TmdUserDatabaseEditDto();
            BeanUtils.copyBeanProp(tmdUserDatabase, tmdUserDatabaseEditDto);
            SysUser sysUser = sysUserService.selectUserByLoginName(tmdUserDatabase.getUserId());
            TmdDatabase tmdDatabase = tmdDatabaseService.selectTmdDatabaseByDatabaseId(tmdUserDatabase.getDatabaseId());
            tmdUserDatabaseEditDto.setDatabase(tmdDatabase);
            tmdUserDatabaseEditDto.setSysUser(sysUser);
        }
        mmap.put("tmdUserDatabase", tmdUserDatabaseEditDto);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户数据库
     */
    @RequiresPermissions("datamonitor:myUserDatabase:edit")
    @Log(title = "用户数据库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TmdUserDatabase tmdUserDatabase)
    {
        return toAjax(tmdUserDatabaseService.updateTmdUserDatabase(tmdUserDatabase));
    }

    /**
     * 删除用户数据库
     */
    @RequiresPermissions("datamonitor:myUserDatabase:remove")
    @Log(title = "用户数据库", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tmdUserDatabaseService.deleteTmdUserDatabaseByUserDatabaseIds(ids));
    }
}
